package com.easybuy.web.controller.manager;


import com.easybuy.bean.EasybuyUser;
import com.easybuy.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
     private IUserService iNewsService;

    @RequestMapping("/listUserByPage")
    public ModelAndView listUserByPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                   @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<EasybuyUser> userses=iNewsService.listUserByPage(page,pageSize);
        for(EasybuyUser e : userses){
            if(e.getEuSex().equals("T")){
                e.setEuSex("男");
            }else{
                e.setEuSex("女");
            }
            if(e.getEuStatus().equals("1")){
                e.setEuStatus("普通用户");
            }else if(e.getEuStatus().equals("2")){
                e.setEuStatus("管 理 员");
            }

        }

        ModelAndView mv=new ModelAndView();
        mv.addObject("userses",userses);

        mv.addObject("commentPage",page);
        mv.addObject("maxPage",(int)Math.ceil(iNewsService.countAll()/(double)pageSize));


        mv.setViewName("manage/user");
        return mv;
    }

    @RequestMapping("/to_save")
    public String toUserSave(){
        return "manage/user-add";
    }


    @RequestMapping("/commit")
    public String saveUserCommit( EasybuyUser easybuyUser){

        System.out.println("初始数据"+easybuyUser);

        if (easybuyUser.getEuUserId() == null||easybuyUser.getEuUserId().trim().length()==0){
            return "manage/paramEroor";
        }
        if (easybuyUser.getEuUserName() == null||easybuyUser.getEuUserName().trim().length()==0){
            return "manage/paramEroor";
        }
        if (easybuyUser.getEuPassword() == null||easybuyUser.getEuPassword().trim().length()==0){
            return "manage/paramEroor";
        }

        if (easybuyUser.getEuSex() == null||easybuyUser.getEuSex().trim().length()==0){
            return "manage/paramEroor";
        }
        if(easybuyUser.getEuSex().equals("男")){
            easybuyUser.setEuSex("T");
        }
        if(easybuyUser.getEuSex().equals("女")){
            easybuyUser.setEuSex("F");
        }
        easybuyUser.setEuStatus("1");

        System.out.println("修改数据后"+easybuyUser);
        System.out.println("修改后生日"+easybuyUser.getEuBirthday());


        //查询
        EasybuyUser e1=iNewsService.selectByPK(easybuyUser);

        System.out.println("按主键查询后"+e1);
        if((e1!=null&&e1.getEuUserId().equals(easybuyUser.getEuUserId()))){
            System.out.println("判断后不能新增");
            return "manage/paramEroor";
        }
        //保存
        int result=iNewsService.save(easybuyUser);
        if (result<=0){
            //TODO 要么return 要么要出异常
        }
        return "manage/manage-result-user";
       // return null;
    }

    @RequestMapping("/delete/{euUserId}")
    public String delete(@PathVariable("euUserId") String euUserId){
//        int result=iNewsService.(euUserId);
//        if (result<=0){
//            //TODO 要么return 要么要出异常
//        }

        iNewsService.deleteByPK(euUserId);
        System.out.println(euUserId);

        return "manage/manage-result-user";
    }


    @RequestMapping("/updatePage/{euUserId}")
    public String updatePage(Model model,@PathVariable("euUserId") String euUserId){
        System.out.println("传过来的主键"+euUserId);
        EasybuyUser e1=new EasybuyUser();
        e1.setEuUserId(euUserId);
        EasybuyUser easybuyUser=iNewsService.selectByPK(e1);
        //查询后的数据
        System.out.println("查询后的数据"+easybuyUser);

        if(easybuyUser.getEuSex().equals("T")){
            easybuyUser.setEuSex("男");
        }else{
            easybuyUser.setEuSex("女");
        }
        System.out.println("修改后的性别："+easybuyUser.getEuSex());
        model.addAttribute("easybuyUser",easybuyUser);

        return "manage/user-modify";
    }


    @RequestMapping("/updateSubmit")
    public String updateSubmit( EasybuyUser easybuyUser){
        System.out.println("修改内容："+easybuyUser);
        if(easybuyUser.getEuUserId().equals("admin")){
            easybuyUser.setEuStatus("2");
        }else{
            easybuyUser.setEuStatus("1");
        }
        System.out.println("终极修改后"+easybuyUser);
        iNewsService.updatByPK(easybuyUser);

        return "manage/manage-result-user";
    }




}
